package com.smartstate.dataManagement.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smartstate.dataManagement.dto.UserDTO;
import com.smartstate.dataManagement.entity.*;

import org.apache.ibatis.annotations.*;
import org.springframework.security.core.parameters.P;


import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {

  /**
   * 查询部门对应用户数据
   * @param item
   * @return
   */
  List<User> getAllUserAndDepartment(@Param("item") UserDTO<User> item);

  /**
   * 通过用户查角色
   * @param id
   * @return
   */
  String getRoleByUser(@Param("id") String id);

  @Select("select * from user")
  List<User> findAll();

  @Insert("insert into user_role(ID, ROLE_ID, USER_ID) values(#{id}, #{roleId}, #{userId})")
  Boolean addUserRole(@Param("id") String id, @Param("roleId") String roleId, @Param("userId") String userId);

  /**
   * 批量删除用户角色表信息和用户
   */
  int  removeUserRoleByIds(@Param("item") List<String> item);

  /**
   * 删除用户角色表信息和用户
   * @param id
   * @return
   */
  int removeUserRoleById(String id);

  /**
   *  新建一个用户
   */
  int addNewUser(User user);


  /**
   *  根据用户ID更新用户信息
   */
  int updateUserById(User user);


  /**
   *  根据用户ID更新用户角色信息
   */
  int updateRoleById(@Param("roleId") String roleId, @Param("userId") String  userId);



  /**
   *  根据用户名获取角色信息
   */
  List<String> getUserByUserame(String  username,String status);

  /**
   *  根据用户ID获取角色
   */

  List<String> getRoleByUserId(List<String> id);



  @Insert("insert into user_role(ID, ROLE_ID, USER_ID) values(#{id}, #{roleId}, #{userId})")
  int newUserRole(@Param("id") String id, @Param("roleId") String roleId, @Param("userId") String userId);


  @Select(" select  * from  user where id=#{id}")
  User getUser(@Param("id") String id);

  List<User>  getUserByNameorStatus(@Param("username") String username,@Param("status") String status);


  @Select("select * from  user ")
   List<User> getList();


  @Select("select  * from user where username=#{username}")
  User getUserByUsername(@Param("username") String  username);

  @Update("update  user set   state = #{status}, CREATE_USER=#{updateUser},UPDATE_TIME=#{updateTime}    WHERE  id = #{id}")
  int updateStatusById(@Param("id") String id,@Param("status") String status,@Param("updateUser") String updateUser,@Param("updateTime") String updateTime);


 @Select("select * from role_permission  where  role_id=#{roleId}")
  List<RolePermission> getRoleByUserIdSingle(@Param("roleId") String roleId);

  int  addLoginLog(LoginLog loginLog);

  String  getDeptByID(String  deptId);

  String getRoleByName(String useranme);

  @Insert(" INSERT INTO `token_data` (`ID`, `token`, `userId`, `roleId`, `createTime`, `tag`) VALUES (#{id}, #{token}, #{userId}, #{roleId},#{date1},#{tag}) " )
  int  addToken(@Param("id") String id, @Param("token")String token, @Param("userId")String userId, @Param("roleId")String roleId, @Param("date1") Date date1,@Param("tag")String tag);

  User getUserByToken(@Param("token") String token,@Param("userId")String userId);

  @Select("  select  count(1)  from token_data  where  userId=#{id}")
  int getTokenById(@Param("id") String id);


  @Update(" UPDATE `token_data` SET `token` = #{token}  WHERE `userId` = #{id}")
  int updateToken(@Param("id") String id,@Param("token") String token);

  @Update(" update  token_data set roleId=#{roleId}  where userId=#{userId}")
  int  updateRoleByUserId(String userId,String roleId);

  String  getRoleByToken(@Param("token") String token,@Param("userId") String userId,@Param("tag")String tag);

  List<TokenData> getToken(@Param("id") String id,@Param("date1")String date,@Param("tag")String tag);

  @Delete("delete from token_data  where userId=#{id}")
  int  delTokenData(@Param("id") String userId);

  int updateTokenByList(List<TokenData> list,@Param("tag") String tag);

  int updateStates(@Param("roleId")String roleId,@Param("state")String state);

  @Update(" update  user  set password=#{password} where id=#{userId}")
  int updatePassword(@Param("userId") String userId,@Param("password") String password);

  @Select(" select  * from  user_role   where  user_id=#{userId}")
  UserAndRole  getUserAndRole(@Param("userId") String userId);

    @Select(" select  count(1) from  user_role   where  role_id=#{roleId}")
    int  getUserAndRoleByRoleID(@Param("roleId") String roleId);

}

